<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
  <head >
    <title>搜索管理后台 - 总词库管理</title>
	<meta http-equiv="pragma" content="no-cache">
	<meta http-equiv="cache-control" content="no-cache">
	<meta http-equiv="expires" content="0">    
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
	<link rel="stylesheet" type="text/css" href="${request.contextPath}/css/style_manager.css">
	<script type="text/javascript" src="${request.contextPath}/js/jquery-1.4.2.js"></script>
	<script type="text/javascript" src="${request.contextPath}/js/manager.js"></script>
	<script type="text/javascript" src="${request.contextPath}/js/my97/WdatePicker.js"></script>
	<script type="text/javascript" src="${request.contextPath}/js/jquery.overblock-1.2.js"></script>
		
	<script type="text/javascript">
	var list = null;
	$(function(){
		
		$('input.number').bind('keyup', function(){
			$(this).val($(this).val().replace(/[^\d]/g, ''));
		}).bind('blur', function(){
			if($(this).val().length < 1){
				$(this).val(0);
			}
		});
		
		
		list = new SameListObject('listsame', 'dataDiv', 'pageNumDiv');
		list.pp = 10;
		list.init();
		list.list(1, function(){
			initEvent();
		});
	});
	
	function SameListObject(url, listId, pageNumId){
		ListObject.call(this, url, listId, pageNumId);
	}
	/**
	* 通过原型方法，继承了父类的function
	*/
	SameListObject.prototype = new ListObject();
	SameListObject.prototype.success = function(json) {
		var h = '';
		if(json !== null && json.length > 4){
			var obj1 = eval(json)[0];
			this.total = obj1.count;
			var list = obj1.list;
			h = '<table id="listTable" border="0" >' +
				'	<tr>' +
				'		<td class="center title" width="45"><span id="selectAll">选择</span></td>' +
				'		<td class="title sort" onclick="sortData(\'keyWord\');">关键字</td>' +
				'		<td class="center title" style="min-width:50px;">类别</td>' +
				'		<td width="140" class="center title sort" onclick="sortData(\'addTime\');">添加时间</td>' +
				'		<td width="70" class="center title sort">操作</td>' +
				'	</tr>';
			for(var i = 0, len = list.length; i < len; i ++){
				var o = list[i];
				var kid = o.id;
				var kw = o.keyWord || [];
				var cn = o.cate && o.cate.name || '';
				var atime = o.addTimeStr || '';
				
				var whtml = '';
				for(var j = 0, kl = kw.length; j < kl; j += 1){
					var k = kw[j];
					whtml += '<span class="samewordspan"><span class="samewordHtml">'+k+'</span><sup onclick="deleteWordInSame(this);" class="samewordspansup" >×</sup></span>';
				}
				
				
				h +='	<tr class="bgTr">' +
					'		<td class="center"><input type="checkbox" class="checkBoxes" value='+kid+' /></td>' +
					'		<td class="moreOptTd">'+whtml+'</td>' +
					'		<td class="center" style="white-space:nowrap;">'+cn+'</td>' +
					'		<td class="center" style="white-space:nowrap;">'+atime+'</td>' +
					'		<td class="center" style="white-space:nowrap;">' +
					'			<a href="javascript:void(0);" class="aEdit"><span name="_addBtn">添加</span></a>' +
					'			<a href="javascript:void(0);" onclick="deleteData(\''+kid+'\');" class="aDelete">删除</a>' +
					'		</td>' +
					'	</tr>';
			}
			if(list.length < 1){
				h += '<tr><td colspan="8">'+noDataMessage+'</td></tr>';
			}
			h += '</table>';
			$('#'+this.listId).html(h);
		}else{
			h = noDataMessage;
			alert(1);
			$('#'+this.listId).html(h);
		}
	};
	SameListObject.prototype.setParams = function() {
		this.paramObj.cp = this.cp;
		this.paramObj.pp = this.pp;
		
		this.paramObj.keyword = v('#keyword');
		this.paramObj.beginTime = v('#bdate');
		this.paramObj.endTime = v('#edate');
		this.paramObj.sort = v('#sortInput');
		this.paramObj.cateId = v('#allCateSelect');
	};

	var cloneObj = null;
	var oldObj = null;
	var mouseX = 0, mouseY = 0;
	var isMoveDown = false;
	var oldx = 0, oldy = 0;
	var targetDiv = null;
	var halfW = 0, halfH = 0;
	var cateInfoObj = null;
	function initEvent(){
	
		$('#addCate').click(function(){
			$('#addCateInfo').showPop({'blockStyle':{color:'#dddddd'}, 'closeable':false, 'fadeTime':300});
		});
		$('#closeCate').click(function(){
			$('#addCateInfo').hidePop(300);
		});
		
		var floatObj_ = {'blockStyle':{color:'#dddddd'}, 'closeable':false, 'fadeTime':300};
		$('#newSame').click(function(){
			$('#sameWordFloat').showPop(floatObj_);
		});
		$('#industryWordId').click(function(){
			$('#industryWordFloat').showPop(floatObj_);
		});
		
		$('#cswClose').click(function(){
			cswFloat(0);
		});
		
		
		$('.divP').find('span.sameWordkeyspan').live('mousedown', function(e){
			isMoveDown = true;
			var o = $(this);
			var x = e.pageX;
			var y = e.pageY;
			
			oldObj = o;
			oldObj.css('opacity','0.4');
			
			oldx = o.position().left;
			oldy = o.position().top;
			
			mouseX = e.pageX;
			mouseY = e.pageY;
			
			x = oldx;
			y = oldy;

			cloneObj = o.clone();
			$('#newList').append(cloneObj);
			cloneObj.css({
				'position' : 'absolute',
				'left' : x + 'px',
				'top' : y + 'px'
			});
		});
		$('body').live('mouseup', function(e){
			if(targetDiv != null){
				targetDiv.removeClass('tarDivBG');
				targetDiv.append(cloneObj);
				cloneObj.css({
							'opacity' : '1',
							'position' : 'static'
						});
				oldObj.remove();
			}else{
				if(cloneObj){
					cloneObj.remove();
				}
				if(oldObj){
					oldObj.css('opacity', '1');
				}
			}
			checkEmptyExist();
			isMoveDown = false;
		});
		$('#cswFloat').live('mousemove', function(e){
			if(isMoveDown){
				var dEx = e.pageX - mouseX;
				var dEy = e.pageY - mouseY;
				
				var newx = dEx + oldx;
				var newy = dEy + oldy;
				
				cloneObj.css({
					'left' : newx + 'px',
					'top' : newy + 'px'
				});
				targetDiv = null;
				$('.combineDiv').each(function(){
					var o = $(this);
					var px = o.position().left;
					var py = o.position().top;
					var pw = o.width();
					var ph = o.height();
					o.removeClass('tarDivBG');
					if(newx > px && newy > py && newx <= (px + pw) && newy <= (py + ph)){
						targetDiv = o;
						o.addClass('tarDivBG');
						return false;
					}
				});
			}
		});
		
	}
	function ListData(n, func){
		$('#singleDiv').fadeOut(0);
		list.list(n, func);
	}
	var sortType = 1;
	function sortData(f){
		var s = f || '';
		s = (sortType > 0 ? '' : '-') + s;
		sortType = sortType * -1;
		$('#sortInput').val(s);
		ListData(1)
	}
		
	function reset(){
		$('#bdate,#edate,#keyword,#sortInput').val('');
		$("#allCateSelect option:eq(0)").attr('selected', 'selected');
		ListData(1)
	}
	//批量删除
	function batchDelete(){
		var ids = getids();
		if(ids == null || ids.length < 1){
			return;
		}
		if(!confirm("确定删除？")){
			return;
		}
		var url = 'delsames?id=' + ids;
		ajax(url, function(rs){
			if(rs == 1){
				s = '批量删除成功！';
			}else{
				s = '批量删除失败！';
			}
			showRs(s);
			ListData()
		});
	}
	
	function deleteData(id){
		if(!confirm("确定删除？")){
			return;
		}
		var url = 'delsames?id=' + id;
		ajax(url, function(rs){
			if(rs == 1){
				s = '删除成功！';
			}else{
				s = '删除失败！';
			}
			showRs(s);
			ListData()
		});
	}
	
	function deleteWordInSame(obj){
		var o = $(obj);
		var k = o.parent('.samewordspan').find('.samewordHtml').html();
		k = encodeURI(k);
		var id = o.parents('.bgTr').find('.checkBoxes').val();
		var url = 'delsame?id=' + id + '&keyword=' + k;
		ajax(url, function(rs){
			if(rs == 1){
				s = '删除成功！';
			}else{
				s = '删除失败！';
			}
			showRs(s);
			ListData()
		});
	}
	
	function doBatchCate(){
		var ids = getids();
		if(ids == null || ids.length < 1){
			return;
		}
		var cateid = $('#cateInfoHTML #allCateSelect').val();
		ajax('sameSetCate?id='+ids+'&cateid='+cateid, function(rs){
			reloadPage();
		});
	}
	
	function batchCombine(){
		var ids = getids();
		if(ids == null || ids.length < 1){
			return;
		}
		
		showCombineData(ids, function(rs){
			
		});
		cswFloat(1);
	}
	//显示待合并的同义词信息
	function showCombineData(ids, func){
		cateInfoObj = $('#allCateSelect').clone();
		cateInfoObj.removeAttr('id')

		func = func || function(){};
		$('#newList').html('<div class="divP"><div id="defaultC" class="combineDiv"></div><div class="combineDivCate"></div></div>');
		ajax('listsamebyids?id=' + ids, function(rs){
			if(rs && rs.length > 4){
				var list = eval(rs);
				var ids = '';
				var h = '';
				for(var i = 0, len = list.length; i < len; i ++){
					var o = list[i];
					var id = o.id;
					var ks = o.keyWord;
					ids += id + ',';
					
					for(var j = 0, jl = ks.length; j < jl; j ++){
						var k = ks[j];
						h += '<span class="sameWordkeyspan">' + k + '</span>';
					}
					
				}
				$('#defaultC').html('').html(h);			
				getCateAfterCombine($('#defaultC'));
				$('#newList').append('<div class="divP"><div class="combineDiv"></div><div class="combineDivCate"></div></div>');
				getCateAfterCombine($('#newList').find('.combineDiv').eq(1));
			}
			func(rs);
		});
	}
	
	function getCateAfterCombine(obj){
		var newSelectorDiv = obj.parent('div.divP').find('div.combineDivCate');
		newSelectorDiv.html('').append(cateInfoObj.clone());
	}
	
	function cswFloat(n){
		if(n && n > 0){
			$('#cswFloat').showPop({'blockStyle':{color:'#dddddd'}, 'closeable':false, 'fadeTime':300});
		}else{
			$('#cswFloat').hidePop();
		}
	}
	
	function checkEmptyExist(){
		var nl = $('#newList');
		var allowNew = true;
		nl.find('.divP').each(function(){
			var o = $(this);
			if(o.find('div.combineDiv').find('*').length == 0){
				if(!allowNew){
					o.remove();
				}
				allowNew = false;
			}
		});
		if(allowNew && nl.find('.divP').length < 6){
			nl.append('<div class="divP"><div class="combineDiv"></div><div class="combineDivCate"></div></div>');
			getCateAfterCombine(nl.find('div.divP').find('div.combineDiv').last());
		}
	}
	function confirmCombine(){
		var newSamekey = [];
		var isOne = false;
		$('#newList .divP').each(function(){
			var o = $(this).find('div.combineDiv');
			var c = $(this).find('div.combineDivCate');
			var spans = o.find('.sameWordkeyspan');
			if(spans.length > 0){
				if(spans.length == 1){
					isOne = true;
				}
				var ks = spans.map(function(){return $(this).html();}).get().join(',');
				var cateid = c.find('select').val();
				var newsimple = {};
				newsimple.keys = ks;
				newsimple.cateId = cateid;
				newSamekey.push(newsimple);
			}
		});
		if(isOne && !confirm('只有一个关键词的词条将不被保持，是否继续？')){
			return;
		}
		var jsonString = jsonToString(newSamekey);
		jsonString = encodeURI(jsonString);
		ajax('samecombine?swString=' + jsonString + '&id=' + getids(), function(rs){
			reloadPage();
		});
	}
	</script>
	<style type="text/css">
	#table1 td{
		font-size:13px;
	}

	input.number{
		border-radius:3px;
		border:1px solid gray;
		height:22px;
		padding-left:5px;
	}
	input.btn{
		border-radius:5px;height:25px;border:1px solid #aaaaaa;
	}
	input.inputBox{
		border-radius:4px;
		border:1px solid #aaaaaa;
		height:23px;
		padding-left:5px;
	}
	.tarDivBG{
		background-color:#B3E568;
	}
	
	</style>
  </head>
  <body>
  	<div id="containerDiv">
  		<div style="">
			<h3>同义词库管理</h2>
  		</div>
  		<div style="font-size:13px;padding:5p;margin-bottom:20px;">
			日期：从
			<input type="text" class="inputBox" value="" readOnly="readOnly" onclick="WdatePicker({el:'bdate',dateFmt:'yyyy-MM-dd HH:mm:ss'});" id="bdate" size="16" />
			到
			<input type="text" class="inputBox" value="" readOnly="readOnly" id="edate" size="16" onclick="WdatePicker({el:'edate',dateFmt:'yyyy-MM-dd HH:mm:ss'});" />
			&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;类别：
			<select style="border-radius:4px;height:23px;" id="allCateSelect">
				<option value="">全部</option>
			</select>
			<span id="addCate" class="addBtn">+</span>
			
			<div style="margin-top:5px;">
				关键字：&nbsp;<input type="text" class="inputBox" id="keyword" value="" size="50" />&nbsp;&nbsp;
				<input type="button" value="搜索" class="btnStyle1" onclick="ListData(1);" />&nbsp;&nbsp;
				<input type="button" value="重置" style="background-color:red;" class="btnStyle1"  onclick="reset();" />
				<input type="button" value="刷新" class="btnStyle1" onclick="reloadPage();" />
				&nbsp;&nbsp;&nbsp;&nbsp;
			  	<input type="hidden" id="sortInput" value="" />
			  	<input type="button" style="float:right;margin-right:120px;" value="新增同义词" class="btnStyle1" id="newSame" />
			</div>
  		</div>
  		<div>
  		</div>
	  	<div id="dataDiv">
	  		
	  	</div>
	  	<div id="pageNumDiv" style="padding-right:-20px;">
	  		
	  	</div>
		  	<input type="button" value="删除所选" id="batchDel" onclick="batchDelete();" class="btnStyle1" />
		  	<input type="button" value="指定类别" id="batchCate" onclick="batchCate();" class="btnStyle1" />
		  	<input type="button" value="合并拆分同义词" id="batchCombine" onclick="batchCombine();" class="btnStyle1" />
		  	<span id="batchResult" style="color:red;font-weight:bold;margin-left:230px;">&nbsp;</span>
  	</div>
  	
	<div id="cswFloat" onselectstart="return false" class="wordFloat" style="width:600px;height:550px;">
		<div id="cswTitleDiv" class="titleDiv">
		  	<div id="cswClose" class="closeDiv" >×</div>
		  	<div style="float:left;margin-top:-5px;">合并同义词</div>
		</div>
		<div style="width:465px;height:25px;font-size:12px;margin-bottom:-25px;padding-left:15px;color:red;">
			拖动关键词放置到矩形框完成操作（蓝色框表示新词条，红色表示丢弃关键词）
		</div>
		<div id="newList" style="padding-top:15px;padding-left:15px;padding-right:15px;padding-bottom:4px;">
			<div class="divP">
				<div id="defaultC" class="combineDiv"></div>
				<div class="combineDivCate"></div>
			</div>
		</div>
		<div style="padding-left:15px;">
			<div class="divP">
				<div id="deleteC" class="combineDiv" style="background-color:#FF1A1A;"></div>
			</div>
		</div>
		<div style="clear:both;"></div>
		<div style="padding:20px;">
		  	<input type="button" value="确认" id="confirmCombine" onclick="confirmCombine();" class="btnStyle1" />
		</div>
	</div>
		
  	
  	<input type="hidden" value="添加同义词库类别" id="cateTitleInput" />
  	<input type="hidden" value="same_word" id="CATEINFOTYPE" />
  	<#include "./cate_float.ftl" />
  	<#include "./same_word.ftl" />
  </body>
</html>
